<div class="body">
            
    <div class="section" id="id1">
  <h1>快速入门</h1>
  <div class="admonition note">
  <p class="first admonition-title">注解</p>
  <p class="last">译者注：编程名词和专业名词尽量不翻译，比如bands, modes等请参考 <tt class="xref doc docutils literal"><span class="pre">概念</span></tt> 。</p>
  </div>
  <div class="section" id="image">
  <h2>使用 Image 类</h2>
  <p>PIL最重要的类是
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">Image</span></tt></a> class, 你可以通过多种方法创建这个类的实例；你可以从文件加载图像，或者处理其他图像, 或者从 scratch 创建。</p>
  <p>要从文件加载图像，使用 <a class="reference internal" href="../reference/Image.html#PIL.Image.open" title="PIL.Image.open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> 函数，
  在 <a class="reference internal" href="../reference/Image.html#module-PIL.Image" title="PIL.Image"><tt class="xref py py-mod docutils literal"><span class="pre">Image</span></tt></a> 模块:</p>
  <pre><code class="language-python"><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  <span class="gp">&gt;&gt;&gt; </span><span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">"lena.ppm"</span><span class="p">)</span>
  </code></pre>
  <p>加载成功将返回一个 <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">Image</span></tt></a> 对象。
  你现在可以使用示例属性检查文件内容:</p>
  <pre><code class="language-python"><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  <span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="n">im</span><span class="o">.</span><span class="n">format</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">mode</span><span class="p">)</span>
  <span class="go">PPM (512, 512) RGB</span>
  </code></pre>
  <p><tt class="xref py py-attr docutils literal"><span class="pre">format</span></tt> 这个属性标识了图像来源。如果图像不是从文件读取它的值就是None。size属性是一个二元tuple，包含width和height（宽度和高度，单位都是px）。
  <tt class="xref py py-attr docutils literal"><span class="pre">mode</span></tt> 属性定义了图像bands的数量和名称，以及像素类型和深度。常见的modes 有 “L”
  (luminance) 表示灰度图像, “RGB” 表示真彩色图像, and “CMYK” 表示出版图像。</p>
  <p>如果文件打开错误，返回 <tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt> 错误。</p>
  <p>只要你有了 <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">Image</span></tt></a> 类的实例，你就可以通过类的方法处理图像。比如，下列方法可以显示图像:</p>
  <pre><code class="language-python"><span class="gp">&gt;&gt;&gt; </span><span class="n">im</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
  </code></pre>
  <div class="admonition note">
  <p class="first admonition-title">注解</p>
  <p class="last">标准的 <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.show" title="PIL.Image.Image.show"><tt class="xref py py-meth docutils literal"><span class="pre">show()</span></tt></a> 效率并不高，它需要保存图像到临时文件然后通过
  <strong class="command">xv</strong> 显示图像。你需要先安装 <strong class="command">xv</strong> ，显示图像有助于调试和测试。</p>
  </div>
  <p>下面的部分提供了这个库其他函数的概览。</p>
  </div>
  <div class="section" id="id2">
  <h2>读写图像</h2>
  <p>PIL 模块支持大量图片格式。使用在
  <a class="reference internal" href="../reference/Image.html#module-PIL.Image" title="PIL.Image"><tt class="xref py py-mod docutils literal"><span class="pre">Image</span></tt></a> 模块的 <a class="reference internal" href="../reference/Image.html#PIL.Image.open" title="PIL.Image.open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> 函数从磁盘读取文件。你不需要知道文件格式就能打开它，这个库能够根据文件内容自动确定文件格式。</p>
  <p>要保存文件，使用
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">Image</span></tt></a> 类的 <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.save" title="PIL.Image.Image.save"><tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt></a> 方法。保存文件的时候文件名变得重要了。除非你指定格式，否则这个库将会以文件名的扩展名作为格式保存。</p>
  <div class="section" id="jpeg">
  <h3>转换文件格式到JPEG</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  <span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>
  <span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  
  <span class="k">for</span> <span class="n">infile</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
      <span class="n">f</span><span class="p">,</span> <span class="n">e</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">infile</span><span class="p">)</span>
      <span class="n">outfile</span> <span class="o">=</span> <span class="n">f</span> <span class="o">+</span> <span class="s">".jpg"</span>
      <span class="k">if</span> <span class="n">infile</span> <span class="o">!=</span> <span class="n">outfile</span><span class="p">:</span>
          <span class="k">try</span><span class="p">:</span>
              <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">infile</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">outfile</span><span class="p">)</span>
          <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
              <span class="k">print</span><span class="p">(</span><span class="s">"cannot convert"</span><span class="p">,</span> <span class="n">infile</span><span class="p">)</span>
  </code></pre>
  <p><a class="reference internal" href="../reference/Image.html#PIL.Image.Image.save" title="PIL.Image.Image.save"><tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt></a> 方法的第二个参数可以指定文件格式，如果你使用非标准的扩展名你必须这样做：</p>
  </div>
  <div class="section" id="id3">
  <h3>创建 JPEG 缩略图</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  <span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>
  <span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  
  <span class="n">size</span> <span class="o">=</span> <span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
  
  <span class="k">for</span> <span class="n">infile</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
      <span class="n">outfile</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">infile</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s">".thumbnail"</span>
      <span class="k">if</span> <span class="n">infile</span> <span class="o">!=</span> <span class="n">outfile</span><span class="p">:</span>
          <span class="k">try</span><span class="p">:</span>
              <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">infile</span><span class="p">)</span>
              <span class="n">im</span><span class="o">.</span><span class="n">thumbnail</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
              <span class="n">im</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="s">"JPEG"</span><span class="p">)</span>
          <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
              <span class="k">print</span><span class="p">(</span><span class="s">"cannot create thumbnail for"</span><span class="p">,</span> <span class="n">infile</span><span class="p">)</span>
  </code></pre>
  <p>很重要的一点是这个库不会直接解码或者加载图像栅格数据。当你打开一个文件，只会读取文件头信息用来确定格式，颜色模式，大小等等，文件的剩余部分不会主动处理。这意味着打开一个图像文件的操作十分快速，跟图片大小和压缩方式无关。下面是一个简单的脚本用来快速验证大量图片。</p>
  </div>
  <div class="section" id="id4">
  <h3>验证图像文件</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  <span class="kn">import</span> <span class="nn">sys</span>
  <span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  
  <span class="k">for</span> <span class="n">infile</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]:</span>
      <span class="k">try</span><span class="p">:</span>
          <span class="k">with</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">infile</span><span class="p">)</span> <span class="k">as</span> <span class="n">im</span><span class="p">:</span>
              <span class="k">print</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">format</span><span class="p">,</span> <span class="s">"</span><span class="si">%d</span><span class="s">x</span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="n">im</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">mode</span><span class="p">)</span>
      <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
          <span class="k">pass</span>
  </code></pre>
  </div>
  </div>
  <div class="section" id="id5">
  <h2>剪切，粘贴，合并图像</h2>
  <p><a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">Image</span></tt></a> 类包含的方法允许你操作图像部分选区。使用:py:meth:<cite>~PIL.Image.Image.crop</cite> 方法获取图像的一个子矩形选区。</p>
  <div class="section" id="id6">
  <h3>从图像中复制出一个矩形选区</h3>
  <pre><code class="language-python"><span class="n">box</span> <span class="o">=</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">400</span><span class="p">,</span> <span class="mi">400</span><span class="p">)</span>
  <span class="n">region</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">crop</span><span class="p">(</span><span class="n">box</span><span class="p">)</span>
  </code></pre>
  <p>矩形选区有一个4元元组定义，分别表示左、上、右、下的坐标。这个库以左上角为坐标原点，单位是px，所以上诉代码复制了一个 300x300 pixels 的矩形选区。这个选区现在可以被处理并且粘贴到原图。</p>
  </div>
  <div class="section" id="id7">
  <h3>处理复制的矩形选区并粘贴到原图</h3>
  <pre><code class="language-python"><span class="n">region</span> <span class="o">=</span> <span class="n">region</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">ROTATE_180</span><span class="p">)</span>
  <span class="n">im</span><span class="o">.</span><span class="n">paste</span><span class="p">(</span><span class="n">region</span><span class="p">,</span> <span class="n">box</span><span class="p">)</span>
  </code></pre>
  <p>当你粘贴矩形选区的时候必须保证尺寸一致。此外，矩形选区不能在图像外。然而你不必保证矩形选区和原图的颜色模式一致，因为矩形选区会被自动转换颜色（参看下面的 <a class="reference internal" href="#color-transforms"><em>颜色变换</em></a>  部分），下面是一个例子：</p>
  </div>
  <div class="section" id="rolling-an-image">
  <h3>Rolling an image</h3>
  <pre><code class="language-python"><span class="k">def</span> <span class="nf">roll</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">delta</span><span class="p">):</span>
      <span class="s">"Roll an image sideways"</span>
  
      <span class="n">xsize</span><span class="p">,</span> <span class="n">ysize</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">size</span>
  
      <span class="n">delta</span> <span class="o">=</span> <span class="n">delta</span> <span class="o">%</span> <span class="n">xsize</span>
      <span class="k">if</span> <span class="n">delta</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="k">return</span> <span class="n">image</span>
  
      <span class="n">part1</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">crop</span><span class="p">((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">ysize</span><span class="p">))</span>
      <span class="n">part2</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">crop</span><span class="p">((</span><span class="n">delta</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">xsize</span><span class="p">,</span> <span class="n">ysize</span><span class="p">))</span>
      <span class="n">image</span><span class="o">.</span><span class="n">paste</span><span class="p">(</span><span class="n">part2</span><span class="p">,</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">xsize</span><span class="o">-</span><span class="n">delta</span><span class="p">,</span> <span class="n">ysize</span><span class="p">))</span>
      <span class="n">image</span><span class="o">.</span><span class="n">paste</span><span class="p">(</span><span class="n">part1</span><span class="p">,</span> <span class="p">(</span><span class="n">xsize</span><span class="o">-</span><span class="n">delta</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">xsize</span><span class="p">,</span> <span class="n">ysize</span><span class="p">))</span>
  
      <span class="k">return</span> <span class="n">image</span>
  </code></pre>
  <p>For more advanced tricks, the paste method can also take a transparency mask as
  an optional argument. In this mask, the value 255 indicates that the pasted
  image is opaque in that position (that is, the pasted image should be used as
  is). The value 0 means that the pasted image is completely transparent. Values
  in-between indicate different levels of transparency.</p>
  <p>The Python Imaging Library also allows you to work with the individual bands of
  an multi-band image, such as an RGB image. The split method creates a set of
  new images, each containing one band from the original multi-band image. The
  merge function takes a mode and a tuple of images, and combines them into a new
  image. The following sample swaps the three bands of an RGB image:</p>
  </div>
  <div class="section" id="id8">
  <h3>分离和合并颜色通道</h3>
  <pre><code class="language-python"><span class="n">r</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="s">"RGB"</span><span class="p">,</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">g</span><span class="p">,</span> <span class="n">r</span><span class="p">))</span>
  </code></pre>
  <p>Note that for a single-band image, <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.split" title="PIL.Image.Image.split"><tt class="xref py py-meth docutils literal"><span class="pre">split()</span></tt></a> returns
  the image itself. To work with individual color bands, you may want to convert
  the image to “RGB” first.</p>
  </div>
  </div>
  <div class="section" id="id9">
  <h2>几何变换</h2>
  <p>The <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">PIL.Image.Image</span></tt></a> class contains methods to
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.resize" title="PIL.Image.Image.resize"><tt class="xref py py-meth docutils literal"><span class="pre">resize()</span></tt></a> and <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.rotate" title="PIL.Image.Image.rotate"><tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt></a> an
  image. The former takes a tuple giving the new size, the latter the angle in
  degrees counter-clockwise.</p>
  <div class="section" id="id10">
  <h3>简单的几何变换</h3>
  <pre><code class="language-python"><span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">128</span><span class="p">,</span> <span class="mi">128</span><span class="p">))</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">rotate</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span> <span class="c"># degrees counter-clockwise</span>
  </code></pre>
  <p>To rotate the image in 90 degree steps, you can either use the
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.rotate" title="PIL.Image.Image.rotate"><tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt></a> method or the
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.transpose" title="PIL.Image.Image.transpose"><tt class="xref py py-meth docutils literal"><span class="pre">transpose()</span></tt></a> method. The latter can also be used to
  flip an image around its horizontal or vertical axis.</p>
  </div>
  <div class="section" id="id11">
  <h3>旋转图像</h3>
  <pre><code class="language-python"><span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">FLIP_LEFT_RIGHT</span><span class="p">)</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">FLIP_TOP_BOTTOM</span><span class="p">)</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">ROTATE_90</span><span class="p">)</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">ROTATE_180</span><span class="p">)</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="n">Image</span><span class="o">.</span><span class="n">ROTATE_270</span><span class="p">)</span>
  </code></pre>
  <p>There’s no difference in performance or result between <tt class="docutils literal"><span class="pre">transpose(ROTATE)</span></tt>
  and corresponding <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.rotate" title="PIL.Image.Image.rotate"><tt class="xref py py-meth docutils literal"><span class="pre">rotate()</span></tt></a> operations.</p>
  <p>A more general form of image transformations can be carried out via the
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.transform" title="PIL.Image.Image.transform"><tt class="xref py py-meth docutils literal"><span class="pre">transform()</span></tt></a> method.</p>
  </div>
  </div>
  <div class="section" id="color-transforms">
  <span id="id12"></span><h2>颜色变换</h2>
  <p>The Python Imaging Library allows you to convert images between different pixel
  representations using the <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.convert" title="PIL.Image.Image.convert"><tt class="xref py py-meth docutils literal"><span class="pre">convert()</span></tt></a> method.</p>
  <div class="section" id="id13">
  <h3>颜色模式转换</h3>
  <pre><code class="language-python"><span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">"lena.ppm"</span><span class="p">)</span><span class="o">.</span><span class="n">convert</span><span class="p">(</span><span class="s">"L"</span><span class="p">)</span>
  </code></pre>
  <p>The library supports transformations between each supported mode and the “L”
  and “RGB” modes. To convert between other modes, you may have to use an
  intermediate image (typically an “RGB” image).</p>
  </div>
  </div>
  <div class="section" id="id14">
  <h2>颜色增强</h2>
  <p>The Python Imaging Library provides a number of methods and modules that can be
  used to enhance images.</p>
  <div class="section" id="id15">
  <h3>过滤器</h3>
  <p>The <a class="reference internal" href="../reference/ImageFilter.html#module-PIL.ImageFilter" title="PIL.ImageFilter"><tt class="xref py py-mod docutils literal"><span class="pre">ImageFilter</span></tt></a> module contains a number of pre-defined
  enhancement filters that can be used with the
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.filter" title="PIL.Image.Image.filter"><tt class="xref py py-meth docutils literal"><span class="pre">filter()</span></tt></a> method.</p>
  <div class="section" id="id16">
  <h4>应用过滤器</h4>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">ImageFilter</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">ImageFilter</span><span class="o">.</span><span class="n">DETAIL</span><span class="p">)</span>
  </code></pre>
  </div>
  </div>
  <div class="section" id="id17">
  <h3>点操作</h3>
  <p>The <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.point" title="PIL.Image.Image.point"><tt class="xref py py-meth docutils literal"><span class="pre">point()</span></tt></a> method can be used to translate the pixel
  values of an image (e.g. image contrast manipulation). In most cases, a
  function object expecting one argument can be passed to the this method. Each
  pixel is processed according to that function:</p>
  <div class="section" id="id18">
  <h4>应用点操作</h4>
  <pre><code class="language-python"><span class="c"># multiply each pixel by 1.2</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">point</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">*</span> <span class="mf">1.2</span><span class="p">)</span>
  </code></pre>
  <p>Using the above technique, you can quickly apply any simple expression to an
  image. You can also combine the <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.point" title="PIL.Image.Image.point"><tt class="xref py py-meth docutils literal"><span class="pre">point()</span></tt></a> and
  <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.paste" title="PIL.Image.Image.paste"><tt class="xref py py-meth docutils literal"><span class="pre">paste()</span></tt></a> methods to selectively modify an image:</p>
  </div>
  <div class="section" id="bands">
  <h4>处理个别bands</h4>
  <pre><code class="language-python"><span class="c"># split the image into individual bands</span>
  <span class="n">source</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
  
  <span class="n">R</span><span class="p">,</span> <span class="n">G</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span>
  
  <span class="c"># select regions where red is less than 100</span>
  <span class="n">mask</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="n">R</span><span class="p">]</span><span class="o">.</span><span class="n">point</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">100</span> <span class="ow">and</span> <span class="mi">255</span><span class="p">)</span>
  
  <span class="c"># process the green band</span>
  <span class="n">out</span> <span class="o">=</span> <span class="n">source</span><span class="p">[</span><span class="n">G</span><span class="p">]</span><span class="o">.</span><span class="n">point</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">*</span> <span class="mf">0.7</span><span class="p">)</span>
  
  <span class="c"># paste the processed band back, but only where red was &lt; 100</span>
  <span class="n">source</span><span class="p">[</span><span class="n">G</span><span class="p">]</span><span class="o">.</span><span class="n">paste</span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="bp">None</span><span class="p">,</span> <span class="n">mask</span><span class="p">)</span>
  
  <span class="c"># build a new multiband image</span>
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">im</span><span class="o">.</span><span class="n">mode</span><span class="p">,</span> <span class="n">source</span><span class="p">)</span>
  </code></pre>
  <p>Note the syntax used to create the mask:</p>
  <pre><code class="language-python"><span class="n">imout</span> <span class="o">=</span> <span class="n">im</span><span class="o">.</span><span class="n">point</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">expression</span> <span class="ow">and</span> <span class="mi">255</span><span class="p">)</span>
  </code></pre>
  <p>Python only evaluates the portion of a logical expression as is necessary to
  determine the outcome, and returns the last value examined as the result of the
  expression. So if the expression above is false (0), Python does not look at
  the second operand, and thus returns 0. Otherwise, it returns 255.</p>
  </div>
  </div>
  <div class="section" id="id19">
  <h3>增强</h3>
  <p>For more advanced image enhancement, you can use the classes in the
  <a class="reference internal" href="../reference/ImageEnhance.html#module-PIL.ImageEnhance" title="PIL.ImageEnhance"><tt class="xref py py-mod docutils literal"><span class="pre">ImageEnhance</span></tt></a> module. Once created from an image, an enhancement
  object can be used to quickly try out different settings.</p>
  <p>You can adjust contrast, brightness, color balance and sharpness in this way.</p>
  <div class="section" id="id20">
  <h4>增强图形</h4>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">ImageEnhance</span>
  
  <span class="n">enh</span> <span class="o">=</span> <span class="n">ImageEnhance</span><span class="o">.</span><span class="n">Contrast</span><span class="p">(</span><span class="n">im</span><span class="p">)</span>
  <span class="n">enh</span><span class="o">.</span><span class="n">enhance</span><span class="p">(</span><span class="mf">1.3</span><span class="p">)</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="s">"30% more contrast"</span><span class="p">)</span>
  </code></pre>
  </div>
  </div>
  </div>
  <div class="section" id="id21">
  <h2>多帧图像序列</h2>
  <p>The Python Imaging Library contains some basic support for image sequences
  (also called animation formats). Supported sequence formats include FLI/FLC,
  GIF, and a few experimental formats. TIFF files can also contain more than one
  frame.</p>
  <p>When you open a sequence file, PIL automatically loads the first frame in the
  sequence. You can use the seek and tell methods to move between different
  frames:</p>
  <div class="section" id="id22">
  <h3>读取序列</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">"animation.gif"</span><span class="p">)</span>
  <span class="n">im</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c"># skip to the second frame</span>
  
  <span class="k">try</span><span class="p">:</span>
      <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
          <span class="n">im</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="n">im</span><span class="o">.</span><span class="n">tell</span><span class="p">()</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span>
          <span class="c"># do something to im</span>
  <span class="k">except</span> <span class="ne">EOFError</span><span class="p">:</span>
      <span class="k">pass</span> <span class="c"># end of sequence</span>
  </code></pre>
  <p>As seen in this example, you’ll get an <tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt> exception when the
  sequence ends.</p>
  <p>Note that most drivers in the current version of the library only allow you to
  seek to the next frame (as in the above example). To rewind the file, you may
  have to reopen it.</p>
  <p>The following iterator class lets you to use the for-statement to loop over the
  sequence:</p>
  </div>
  <div class="section" id="class">
  <h3>序列迭代 class</h3>
  <div class="highlight-python"><pre>class ImageSequence:
      def __init__(self, im):
          self.im = im
      def __getitem__(self, ix):
          try:
              if ix:
                  self.im.seek(ix)
              return self.im
          except EOFError:
              raise IndexError # end of sequence
  
  for frame in ImageSequence(im):
      # ...do something to frame...</pre>
  </div>
  </div>
  </div>
  <div class="section" id="postscript">
  <h2>Postscript 打印</h2>
  <p>The Python Imaging Library includes functions to print images, text and
  graphics on Postscript printers. Here’s a simple example:</p>
  <div class="section" id="drawing-postscript">
  <h3>Drawing Postscript</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>
  <span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">PSDraw</span>
  
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">"lena.ppm"</span><span class="p">)</span>
  <span class="n">title</span> <span class="o">=</span> <span class="s">"lena"</span>
  <span class="n">box</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">*</span><span class="mi">72</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">72</span><span class="p">,</span> <span class="mi">7</span><span class="o">*</span><span class="mi">72</span><span class="p">,</span> <span class="mi">10</span><span class="o">*</span><span class="mi">72</span><span class="p">)</span> <span class="c"># in points</span>
  
  <span class="n">ps</span> <span class="o">=</span> <span class="n">PSDraw</span><span class="o">.</span><span class="n">PSDraw</span><span class="p">()</span> <span class="c"># default is sys.stdout</span>
  <span class="n">ps</span><span class="o">.</span><span class="n">begin_document</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>
  
  <span class="c"># draw the image (75 dpi)</span>
  <span class="n">ps</span><span class="o">.</span><span class="n">image</span><span class="p">(</span><span class="n">box</span><span class="p">,</span> <span class="n">im</span><span class="p">,</span> <span class="mi">75</span><span class="p">)</span>
  <span class="n">ps</span><span class="o">.</span><span class="n">rectangle</span><span class="p">(</span><span class="n">box</span><span class="p">)</span>
  
  <span class="c"># draw centered title</span>
  <span class="n">ps</span><span class="o">.</span><span class="n">setfont</span><span class="p">(</span><span class="s">"HelveticaNarrow-Bold"</span><span class="p">,</span> <span class="mi">36</span><span class="p">)</span>
  <span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">ps</span><span class="o">.</span><span class="n">textsize</span><span class="p">(</span><span class="n">title</span><span class="p">)</span>
  <span class="n">ps</span><span class="o">.</span><span class="n">text</span><span class="p">((</span><span class="mi">4</span><span class="o">*</span><span class="mi">72</span><span class="o">-</span><span class="n">w</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="o">*</span><span class="mi">72</span><span class="o">-</span><span class="n">h</span><span class="p">),</span> <span class="n">title</span><span class="p">)</span>
  
  <span class="n">ps</span><span class="o">.</span><span class="n">end_document</span><span class="p">()</span>
  </code></pre>
  </div>
  </div>
  <div class="section" id="id23">
  <h2>更多关于读取图像</h2>
  <p>As described earlier, the <a class="reference internal" href="../reference/Image.html#PIL.Image.open" title="PIL.Image.open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> function of the
  <a class="reference internal" href="../reference/Image.html#module-PIL.Image" title="PIL.Image"><tt class="xref py py-mod docutils literal"><span class="pre">Image</span></tt></a> module is used to open an image file. In most cases, you
  simply pass it the filename as an argument:</p>
  <pre><code class="language-python"><span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">"lena.ppm"</span><span class="p">)</span>
  </code></pre>
  <p>If everything goes well, the result is an <a class="reference internal" href="../reference/Image.html#PIL.Image.Image" title="PIL.Image.Image"><tt class="xref py py-class docutils literal"><span class="pre">PIL.Image.Image</span></tt></a> object.
  Otherwise, an <tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt> exception is raised.</p>
  <p>You can use a file-like object instead of the filename. The object must
  implement <a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#file.read" title="(in Python v2.7)"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a>, <a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#file.seek" title="(in Python v2.7)"><tt class="xref py py-meth docutils literal"><span class="pre">seek()</span></tt></a> and
  <a class="reference external" href="http://docs.python.org/2/library/stdtypes.html#file.tell" title="(in Python v2.7)"><tt class="xref py py-meth docutils literal"><span class="pre">tell()</span></tt></a> methods, and be opened in binary mode.</p>
  <div class="section" id="id24">
  <h3>从文件读取</h3>
  <pre><code class="language-python"><span class="n">fp</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">"lena.ppm"</span><span class="p">,</span> <span class="s">"rb"</span><span class="p">)</span>
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
  </code></pre>
  <p>To read an image from string data, use the <a class="reference external" href="http://docs.python.org/2/library/stringio.html#StringIO.StringIO" title="(in Python v2.7)"><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a>
  class:</p>
  </div>
  <div class="section" id="string">
  <h3>从 string 读取</h3>
  <pre><code class="language-python"><span class="kn">import</span> <span class="nn">StringIO</span>
  
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="nb">buffer</span><span class="p">))</span>
  </code></pre>
  <p>Note that the library rewinds the file (using <tt class="docutils literal"><span class="pre">seek(0)</span></tt>) before reading the
  image header. In addition, seek will also be used when the image data is read
  (by the load method). If the image file is embedded in a larger file, such as a
  tar file, you can use the <a class="reference internal" href="../PIL.html#module-PIL.ContainerIO" title="PIL.ContainerIO"><tt class="xref py py-class docutils literal"><span class="pre">ContainerIO</span></tt></a> or
  <a class="reference internal" href="../PIL.html#module-PIL.TarIO" title="PIL.TarIO"><tt class="xref py py-class docutils literal"><span class="pre">TarIO</span></tt></a> modules to access it.</p>
  </div>
  <div class="section" id="id25">
  <h3>从压缩包读取</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">TarIO</span>
  
  <span class="n">fp</span> <span class="o">=</span> <span class="n">TarIO</span><span class="o">.</span><span class="n">TarIO</span><span class="p">(</span><span class="s">"Imaging.tar"</span><span class="p">,</span> <span class="s">"Imaging/test/lena.ppm"</span><span class="p">)</span>
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
  </code></pre>
  </div>
  </div>
  <div class="section" id="id26">
  <h2>控制解码器</h2>
  <p>Some decoders allow you to manipulate the image while reading it from a file.
  This can often be used to speed up decoding when creating thumbnails (when
  speed is usually more important than quality) and printing to a monochrome
  laser printer (when only a greyscale version of the image is needed).</p>
  <p>The <a class="reference internal" href="../reference/Image.html#PIL.Image.Image.draft" title="PIL.Image.Image.draft"><tt class="xref py py-meth docutils literal"><span class="pre">draft()</span></tt></a> method manipulates an opened but not yet
  loaded image so it as closely as possible matches the given mode and size. This
  is done by reconfiguring the image decoder.</p>
  <div class="section" id="reading-in-draft-mode">
  <h3>Reading in draft mode</h3>
  <pre><code class="language-python"><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
  <span class="n">im</span> <span class="o">=</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
  <span class="k">print</span><span class="p">(</span><span class="s">"original ="</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">mode</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
  
  <span class="n">im</span><span class="o">.</span><span class="n">draft</span><span class="p">(</span><span class="s">"L"</span><span class="p">,</span> <span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">))</span>
  <span class="k">print</span><span class="p">(</span><span class="s">"draft ="</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">mode</span><span class="p">,</span> <span class="n">im</span><span class="o">.</span><span class="n">size</span><span class="p">)</span>
  </code></pre>
  <p>This prints something like:</p>
  <pre><code class="language-python"><span class="n">original</span> <span class="o">=</span> <span class="n">RGB</span> <span class="p">(</span><span class="mi">512</span><span class="p">,</span> <span class="mi">512</span><span class="p">)</span>
  <span class="n">draft</span> <span class="o">=</span> <span class="n">L</span> <span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
  </code></pre>
  <p>Note that the resulting image may not exactly match the requested mode and
  size. To make sure that the image is not larger than the given size, use the
  thumbnail method instead.</p>
  </div>
  </div>
  </div>
  
  
            </div>